package student.aistar.mapper;

import org.apache.ibatis.annotations.*;
import student.aistar.model.entity.Orders;
import student.aistar.model.entity.WebSeckill;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/10/11 20:46
 */
public interface TVSeckillMapper {
    /**
     * 查询还未开始的秒杀商品
     * @return
     */
    @Select("select * from web_seckill where startTime>now() and status = '0'")
    List<Map<String,Object>> beforeActive();

    /**
     * 到达秒杀范围时间的商品状态设置为1
     */
    @Update("update web_seckill set status='1' where startTime<now() and now()<endTime and status='0'")
    void updateState();

    /**
     * 查找范围到秒杀时间的id
     * @return
     */
    @Select("select id from web_seckill where status = '1' and startTime < now() and now() < endTime")
    List<Long> findKillId();

    /**
     * 秒杀结束更新为, 商品状态更新为2
     */
    @Update("update web_seckill set status = '2' where endTime < now() and  startTime < now() and status = '1'")
    void endActive();


    @Select("select id from web_seckill where endTime < now() and status = '2'")
    List<Long> findEndId();

    /**
     * 插入订单表
     * @param
     * @param
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "orderId", keyColumn = "order_id")
    @Insert("insert into orders values(null,#{orderNumber}, #{userId}, 1, 1, 0, 0, 2, '个人', 1, now(), 17999, 17999, 1, 1)")
    int insertOrders(Orders order);


    /**
     * 插入订单详情表
     * @param orderId
     */
    @Insert("insert into orderdetail values(null, #{orderId}, 8, '小米大师', '//cdn.cnbj1.fds.api.mi-img.com/mi-mall/b91c9ef670fa1c12eddf23b209f9ff33.png', 1, '小米大师 65 英寸', 17999, 1, 17999, 0)")
    void insertOrderDetail(Integer orderId);


    /**
     * 查询秒杀商品的秒杀时间返回到也没面
     * @param seckillId
     * @return
     */
    @Select("select startTime, endTime from web_seckill where id = #{seckillId}")
    WebSeckill getStartTimeNow(Integer seckillId);

}
